package middleware

import (
	"errors"
	"gin_zebra/public"
	"github.com/gin-gonic/contrib/sessions"
	"github.com/gin-gonic/gin"
)

func SessionAuthMiddleware() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		session := sessions.Default(ctx)
		if name, ok := session.Get(public.SessionUserKey).(string); !ok || name == "" {
			ResponseError(ctx, errors.New("用户未登录"))
			ctx.Abort()
			return
		}
		ctx.Next()
	}
}
